package longrise.phone.com.bjjt_henan.lunchandlogin;

import android.content.Context;
import android.text.TextUtils;
import android.text.method.PasswordTransformationMethod;
import android.text.method.TransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.FormParameter;
import com.longrise.android.LFView;
import com.longrise.bjjt.BaseApplication;
import com.longrise.bjjt.util.EnCryptDeCrypt;
import com.longrise.bjjt.util.StringUtils;
import com.longrise.bjjt.util.UiUtil;

import longrise.phone.com.bjjt_henan.R;
import longrise.phone.com.bjjt_henan.Utils.ApiImpI;
import longrise.phone.com.bjjt_henan.Utils.CodeCountHelper;
import longrise.phone.com.bjjt_henan.Utils.CommontUtils;
import longrise.phone.com.bjjt_henan.Utils.DialogUtils;
import longrise.phone.com.bjjt_henan.Utils.VerifityHelper;

/**
 * 忘记密码界面
 */
public class ForgetPasswordView extends LFView implements View.OnClickListener {
    private Context context = null;
    private View view = null;

    private EditText phoneET;

    private EditText codeET;

    private TextView getCodeTV;

    private EditText pwdET;

    private EditText cPwdET;

    private Button confirmBtn;

    private BaseApplication baseApplication;

    private VerifityHelper codeHelper;

    private LinearLayout pwdLayout;
    private LinearLayout cpwdLayout;

    private CodeCountHelper helper;

    public ForgetPasswordView(Context c) {
        super(c);
        this.context = c;
    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter fp = new FormParameter();
        fp.setShowtitle(false);
        return fp;
    }

    @Override
    public void refresh() {
        codeHelper.loadPicCode();
    }

    @Override
    public View getView() {
        return view;
    }

    @Override
    public void init() {
        view = View.inflate(context, R.layout.view_forgetpassword, null);
        //标题栏
        TextView titleTV = (TextView) view.findViewById(R.id.tv_title);
        titleTV.setText(R.string.forgot_pwd);
        view.findViewById(R.id.hn_btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OnDestroy();
                closeForm();
            }
        });
        phoneET = (EditText) view.findViewById(R.id.fpwd_et_phone);
        getCodeTV = (TextView) view.findViewById(R.id.fpwd_et_getcode);
        codeET = (EditText) view.findViewById(R.id.fpwd_et_code);
        pwdET = (EditText) view.findViewById(R.id.fpwd_et_newpwd);
        cPwdET = (EditText) view.findViewById(R.id.fpwd_et_cnewphone);
        confirmBtn = (Button) view.findViewById(R.id.fpwd_btn_confirm);
        pwdLayout = (LinearLayout) view.findViewById(R.id.login_layout_pwd);
        cpwdLayout = (LinearLayout) view.findViewById(R.id.login_layout_cpwd);
        setListener(true);

        if (null == codeHelper) {
            codeHelper = new VerifityHelper(view, context);
        }

        baseApplication = (BaseApplication) context.getApplicationContext();

        pwdET.setTransformationMethod(PasswordTransformationMethod.getInstance());
        cPwdET.setTransformationMethod(PasswordTransformationMethod.getInstance());
    }

    public void setListener(boolean flag) {
        if (null != getCodeTV) {
            getCodeTV.setOnClickListener(flag ? this : null);
        }
        if (null != confirmBtn) {
            confirmBtn.setOnClickListener(flag ? this : null);
        }
        if (null != pwdLayout) {
            pwdLayout.setOnClickListener(flag ? this : null);
        }
        if (null != cpwdLayout) {
            cpwdLayout.setOnClickListener(flag ? this : null);
        }
    }

    @Override
    public void onClick(View v) {
        try {
            switch (v.getId()) {
                case R.id.fpwd_et_getcode://获取验证码
                    getCode();
                    break;
                case R.id.fpwd_btn_confirm://确定
                    forgotPwd();
                    break;
                case R.id.login_layout_pwd:
                    CommontUtils.setSeePwd(pwdET);
                    setShowDrawable(pwdET, (ImageView) view.findViewById(R.id.forgot_iv_mpwd));
                    break;
                case R.id.login_layout_cpwd:
                    CommontUtils.setSeePwd(cPwdET);
                    setShowDrawable(cPwdET, (ImageView) view.findViewById(R.id.forgot_iv_cpwd));
                    break;
                default:
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //设置明文密码时的图像标识
    private void setShowDrawable(EditText editText, ImageView imageView) {
        TransformationMethod method = editText.getTransformationMethod();
        if (method instanceof PasswordTransformationMethod) {
            imageView.setImageResource(R.mipmap.eye);
        } else {
            imageView.setImageResource(R.mipmap.eye_select);
        }
    }

    /**
     * 找回密码
     */
    private void forgotPwd() throws Exception {
        if (!codeHelper.checkPicCode()) {
            return;
        }
        String phone = phoneET.getText().toString().trim();
        if (!StringUtils.isPhone(phone)) {
            Toast.makeText(context, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
            return;
        }
        String code = codeET.getText().toString().trim();
        if (TextUtils.isEmpty(code)) {
            Toast.makeText(context, "请输入正确的短信验证码", Toast.LENGTH_SHORT).show();
            return;
        }
        String newPwd = pwdET.getText().toString().trim();
        if (!CommontUtils.isPassword(newPwd)) {
            UiUtil.showToast(context, "请输入6~15位由字母和数字组合的密码！");
            return;
        }
//        String cNewPwd = cPwdET.getText().toString().trim();
//        if (TextUtils.isEmpty(cNewPwd)) {
//            Toast.makeText(context, "请再次输入正确的新密码", Toast.LENGTH_SHORT).show();
//            return;
//        }
//        if (!TextUtils.equals(newPwd, cNewPwd)) {
//            UiUtil.showToast(context, "两次输入的新密码不一致!");
//            return;
//        }
        String depwd = EnCryptDeCrypt.getInstance().EnCrypt(newPwd);//加密

        DialogUtils.showDialog(context);
        ApiImpI.getInstance(baseApplication).forgetPwd("0", phone, code, depwd,
                new ApiImpI.RequestCallBack() {

                    @Override
                    public void onError(String error) {
                        DialogUtils.closeDialog();
                        if (!TextUtils.isEmpty(error)) {
                            UiUtil.showToast(getContext(), error);
                        }
                    }

                    @Override
                    public void onSuccess(Object result) {
                        DialogUtils.closeDialog();
                        EntityBean bean = (EntityBean) result;
                        String redes = bean.getString("redes");
                        if (!TextUtils.isEmpty(redes)) {
                            UiUtil.showToast(getContext(), redes);
                        }
                        String restate = bean.getString("restate");
                        if (TextUtils.equals(restate, "1")) {//修改成功
                            OnDestroy();
                            closeForm();
                        }
                    }
                });
    }

    /**
     * 获取验证码
     */
    public void getCode() {
        if (!codeHelper.checkPicCode()) {
            return;
        }
        String phone = phoneET.getText().toString().trim();
        if (!StringUtils.isPhone(phone)) {
            Toast.makeText(context, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
            return;
        }
        String imgCode = codeHelper.getImgCode();
        String imageId = codeHelper.getPicBean().getString("imgid");
        DialogUtils.showDialog(context);
        ApiImpI.getInstance(baseApplication).getPwdCode(phone, imgCode, imageId,
                new ApiImpI.RequestCallBack() {
                    @Override
                    public void onError(String error) {
                        DialogUtils.closeDialog();
                    }

                    @Override
                    public void onSuccess(Object result) {
                        DialogUtils.closeDialog();
                        EntityBean bean = (EntityBean) result;
                        String redes = bean.getString("redes");
                        if (!TextUtils.isEmpty(redes)) {
                            UiUtil.showToast(getContext(), redes);
                        }
                        String restate = bean.getString("restate");
                        if (TextUtils.equals(restate, "1")) {
                            if (null == view) {
                                return;
                            }
                            if (null == helper) {
                                helper = new CodeCountHelper(getCodeTV);
                            }
                            helper.startCount();
                        }
                    }
                });
    }

    @Override
    public void OnDestroy() {
        super.OnDestroy();
        view = null;
        setListener(false);
        if (null != helper) {
            helper.destroy();
            helper = null;
        }
        codeHelper = null;
    }
}
